<?php
namespace app\home\controller;
use app\home\model\Student;
use app\home\model\Examresult;
use think\Request;
use app\home\model\Paper as Mpaper;
use think\Session;
use app\home\controller\Single;
use app\home\controller\Fillblank;
use app\home\controller\Judge;
use app\home\controller\Shortanswer;

class Paper extends Common
{

    /*
     * 试卷列表
     */
    public function getList(){
        //获取登录学生的stuid
        $stuid = Session::get('id');
        $paperlist = Student::where('stuid',$stuid)->value('paperid');
        $paperlist = explode(',',$paperlist);
//        dump($paperlist);
        $paper = new Mpaper();
        // 查询数据集
        $clist = $paper->getList();
//        dump($clist);
        $this->assign("paperlist",$paperlist);
        $this->assign("clist",$clist);
        return $this->fetch('list');
    }

    /**
     *考试中
     */
    public function examing(){
        if(Request::instance()->isGet()){
            $id = Request::instance()->param('id');
            $paper = new Mpaper();
            $data = $paper->getAllQusetion($id);
            //取出试题列表
//            dump($data);
//            $single = [];
            if($data){
                foreach($data as $key=>$val){
                    switch($key){
                        case 'single';
                            $single = Single::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'mul';
                            $mul = Single::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'ide';
                            $ide = Single::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'fill';
                            $fill = Fillblank::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'judge';
                            $judge = Judge::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'short';
                            $short = Shortanswer::getInfoById($val);
                            unset($data[$key]);
                            break;
                    }
                }
            }
            $this->assign('paper',$data);
            $this->assign('single',$single);
            $this->assign('mul',$mul);
            $this->assign('ide',$ide);
            $this->assign('fill',$fill);
            $this->assign('judge',$judge);
            $this->assign('short',$short);
            $this->assign('paperid',$id);
            return $this->fetch();
        }
    }
    /**
     *考试结果
     */
    public function examResult(){
        if(Request::instance()->isGet()){
            $paperid = Request::instance()->param('id');
            $paper = new Mpaper();
            $data = $paper->getAllQusetion($paperid);
            //取出试题列表
//            dump($data);
//            $single = [];
            if($data){
                foreach($data as $key=>$val){
                    switch($key){
                        case 'single';
                            $single = Single::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'mul';
                            $mul = Single::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'ide';
                            $ide = Single::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'fill';
                            $fill = Fillblank::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'judge';
                            $judge = Judge::getInfoById($val);
                            unset($data[$key]);
                            break;
                        case 'short';
                            $short = Shortanswer::getInfoById($val);
                            unset($data[$key]);
                            break;
                    }
                }
            }
            //查询试卷详细信息
            $examresult = new Examresult();
            $datare = $examresult->getInfo($paperid);
//            dump($datare);
            $this->assign('result',$datare);
            $this->assign('paper',$data);
            $this->assign('single',$single);
            $this->assign('mul',$mul);
            $this->assign('ide',$ide);
            $this->assign('fill',$fill);
            $this->assign('judge',$judge);
            $this->assign('short',$short);
            $this->assign('paperid',$paperid);
            return $this->fetch('examresult');
        }
    }
    /*
     * 阅卷
     * 异步提交答题信息
     */
    public function testPaper(){
        if(Request::instance()->isAjax()){
            $answers_arr = Request::instance()->post();
//            dump($answers_arr);
            $paperid = $answers_arr['paperid'];
            //查询多选和不定项错误扣分
            $p = new \app\home\model\Paper();
            // 查询数据集
            $rule = $p->where('id', $paperid)
                ->field('mulpoint,idepoint')
                ->find();
            //验证答案
            $stuid = Session::get('id');
            $examdetail = [];
            $examdetail['paperid'] = $paperid;
            $examdetail['stuid'] = $stuid;
            $examdetail['singleright'] = 0;
            $examdetail['singleerror'] = 0;
            $examdetail['singleundo'] = 0;
            $examdetail['mulright'] = 0;
            $examdetail['mulerror'] = 0;
            $examdetail['mulundo'] = 0;
            $examdetail['mulscore'] = 0;
            $examdetail['ideright'] = 0;
            $examdetail['ideerror'] = 0;
            $examdetail['ideundo'] = 0;
            $examdetail['idescore'] = 0;
            $examdetail['fillright'] = 0;
            $examdetail['fillerror'] = 0;
            $examdetail['fillundo'] = 0;
            $examdetail['judgeright'] = 0;
            $examdetail['judgeerror'] = 0;
            $examdetail['judgeundo'] = 0;
            $examdetail['shortright'] = 0;
            $examdetail['shorterror'] = 0;
            $examdetail['shortundo'] = 0;
            $examdetail['paperScore'] = 0;
            $examdetail['usetime'] = $answers_arr['usetime'];
            unset($answers_arr['usetime']);
            unset($answers_arr['paperid']);
            foreach($answers_arr as $key=>$val){
                switch($key){
                    case 'single';
                        foreach($val as $k1=>$v1){
                            //取出id为k1的答案和$vi比较
                            if($v1){
                                $sc = Single::getAnswerById($k1);
                                $sc = $sc['answer'];
                                if($sc == $v1){
                                    //做对
                                    $examdetail['singleright'] += 1;
                                    $examdetail['paperScore'] += 2;
                                }else{
                                    //做错
                                    $examdetail['singleerror'] += 1;
                                }
                            }else{
                                //未做
                                $examdetail['singleundo'] += 1;
                            }
                        }
                        break;
                    case 'mul';
                        foreach($val as $k1=>$v1){
                            //取出id为k1的答案和$vi比较
                            if($v1){
                                $sc = Single::getAnswerById($k1);
                                $sc = $sc['answer'];
                                $res = $this->strExist($v1,$sc);
                                if($res == 0){
                                    //做对
                                    $examdetail['mulright'] += 1;
                                    $examdetail['paperScore'] += 3;
                                    $examdetail['mulscore'] += 3;
                                }else if($res > 0){
                                    //漏选
                                    $examdetail['paperScore'] += 3-$res;
                                    $examdetail['mulscore'] += 3-$res;
                                    $examdetail['mulright'] += 1;
                                }else if($res < 0){
                                    //错选 多选
                                    $examdetail['mulerror'] += 1;
                                }
                            }else{
                                //未做
                                $examdetail['mulundo'] += 1;
                            }
                        }
                        break;
                    case 'ide';
                        foreach($val as $k1=>$v1){
                            //取出id为k1的答案和$vi比较
                            if($v1){
                                $sc = Single::getAnswerById($k1);
                                $sc = $sc['answer'];
                                $res = $this->strExist($v1,$sc);
                                if($res == 0){
                                    //做对
                                    $examdetail['ideright'] += 1;
                                    $examdetail['paperScore'] += 4;
                                    $examdetail['idescore'] += 4;
                                }else if($res > 0){
                                    //漏选
                                    $examdetail['paperScore'] += 4-$res;
                                    $examdetail['idescore'] += 4-$res;
                                    $examdetail['ideright'] += 1;
                                }else if($res < 0){
                                    //错选 多选
                                    $examdetail['ideerror'] += 1;
                                }
                            }else{
                                //未做
                                $examdetail['ideundo'] += 1;
                            }
                        }
                        break;
                    case 'fill';
                        foreach($val as $k1=>$v1){
                            if(trim($v1)){
                                //根据$k1查询答案
                                $sc = Fillblank::getAnswerById($k1);
                                $sc = $sc['answer'];
                                if(strpos($sc,'|') === false){
                                    if($sc == $v1){
                                        $examdetail['fillright'] += 1;
                                        $examdetail['paperScore'] += 1;
                                    }else{
                                        $examdetail['fillerror'] += 1;
                                    }
                                }else{
                                    $scarr = explode('|',$sc);
                                    if(in_array($v1,$scarr)){
                                        $examdetail['fillright'] += 1;
                                        $examdetail['paperScore'] += 1;
                                    }else{
                                        $examdetail['fillerror'] += 1;
                                    }
                                }
                            }else{
                                //未做
                                $examdetail['fillundo'] += 1;
                            }
                        }
                        break;
                    case 'judge';
                        foreach($val as $k1=>$v1){
                            if($v1 != ''){
                                $sc = Judge::getAnswerById($k1);
                                $sc = $sc['answer'];
                                if($v1 == $sc){
                                    $examdetail['judgeright'] += 1;
                                    $examdetail['paperScore'] += 1;
                                }else{
                                    $examdetail['judgeerror'] += 1;
                                }
                            }else{
                                //未做
                                $examdetail['judgeundo'] += 1;
                            }
                        }
                        break;
                    case 'short';
                        foreach($val as $k1=>$v1){
                            if(!trim($v1)){
                                //未做
                                $examdetail['shortundo'] += 1;
                            }
                        }
                        break;
                }
            }
//            dump($answers_arr);
//            exit;
            //试卷提交成功 更新Student表的paperid
//            dump($examdetail);
            $mpaper = new \app\home\model\Paper();
            $result = $mpaper->saveExamResult($examdetail,$paperid,$stuid,$answers_arr);
//            dump($result);
//            exit;
            if($result){
                $d['code'] = 1;
                $d['msg'] = $paperid;
                echo json_encode($d);
            }else{
                $d['code'] = 0;
                $d['msg'] = $paperid;
                echo json_encode($d);
            }
        }
    }
    /**
     * 判断第一个字符串是否有第二个字符串没有的
     * @param string1
     * @param string2
     * @return bool
     */
    public function strExist($str1,$str2){
        $error = '';
        $num = 0;
        $str1 = trim($str1,',');
        $str2 = trim($str2,',');
        $str1arr = explode(',',$str1);
        $str2arr = explode(',',$str2);
        //错选
        foreach($str1arr as $v){
            if(!in_array($v,$str2arr)){
                $error = $v;
            }
        }
        if(empty($error)){
            if(count($str1arr) < count($str2arr)){
                //少选
                $num = count($str2arr) - count($str1arr);
            }
        }
        return empty($error) ? $num : -1;
    }
}